package cn.ld.module.system.controller.admin.mail;


import cn.ld.framework.common.pojo.CommonResult;
import cn.ld.framework.common.pojo.PageResult;
import cn.ld.framework.common.util.object.BeanUtils;
import cn.ld.module.system.controller.admin.mail.vo.account.MailAccountPageInVO;
import cn.ld.module.system.controller.admin.mail.vo.account.MailAccountOutVO;
import cn.ld.module.system.controller.admin.mail.vo.account.MailAccountSaveInVO;
import cn.ld.module.system.controller.admin.mail.vo.account.MailAccountSimpleOutVO;
import cn.ld.module.system.dal.entity.mail.MailAccountEntity;
import cn.ld.module.system.service.mail.MailAccountService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import java.util.List;

import static cn.ld.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 邮箱账号")
@RestController
@RequestMapping("/system/mail-account")
public class MailAccountController {

    @Resource
    private MailAccountService mailAccountService;

    @PostMapping("/create")
    @Operation(summary = "创建邮箱账号")
    @PreAuthorize("@ss.hasPermission('system:mail-account:create')")
    public CommonResult<Long> createMailAccount(@Valid @RequestBody MailAccountSaveInVO createInVO) {
        return success(mailAccountService.createMailAccount(createInVO));
    }

    @PutMapping("/update")
    @Operation(summary = "修改邮箱账号")
    @PreAuthorize("@ss.hasPermission('system:mail-account:update')")
    public CommonResult<Boolean> updateMailAccount(@Valid @RequestBody MailAccountSaveInVO updateInVO) {
        mailAccountService.updateMailAccount(updateInVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除邮箱账号")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:mail-account:delete')")
    public CommonResult<Boolean> deleteMailAccount(@RequestParam Long id) {
        mailAccountService.deleteMailAccount(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得邮箱账号")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:mail-account:query')")
    public CommonResult<MailAccountOutVO> getMailAccount(@RequestParam("id") Long id) {
        MailAccountEntity account = mailAccountService.getMailAccount(id);
        return success(BeanUtils.toBean(account, MailAccountOutVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得邮箱账号分页")
    @PreAuthorize("@ss.hasPermission('system:mail-account:query')")
    public CommonResult<PageResult<MailAccountOutVO>> getMailAccountPage(@Valid MailAccountPageInVO pageInVO) {
        PageResult<MailAccountEntity> pageResult = mailAccountService.getMailAccountPage(pageInVO);
        return success(BeanUtils.toBean(pageResult, MailAccountOutVO.class));
    }

    @GetMapping({"/list-all-simple", "simple-list"})
    @Operation(summary = "获得邮箱账号精简列表")
    public CommonResult<List<MailAccountSimpleOutVO>> getSimpleMailAccountList() {
        List<MailAccountEntity> list = mailAccountService.getMailAccountList();
        return success(BeanUtils.toBean(list, MailAccountSimpleOutVO.class));
    }

}
